package com.saturn.action.sldb.emergency;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.DateUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspErrorView;
import com.saturn.app.web.view.JspView;
import com.saturn.auth.User;
import com.saturn.sldb.Emergency;
import com.saturn.sldb.PersonState;

public class SaveAndApplyEmergencyAction implements IAction {
	@Override
	public IView execute(HttpServletRequest request, HttpServletResponse response) {

		String auditStr = request.getParameter("audit");
		String audit = null;
		try {
			audit = new String(auditStr.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String people = request.getParameter("people");
		String month = request.getParameter("month");
		String standard = request.getParameter("standard");
		String money = request.getParameter("money");

		String id = request.getParameter("id");
		Emergency emergency = Emergency.get(id);
		
		User user = (User) request.getSession().getAttribute("authUser");
		
		User userOragantion = User.getOrganizationsByOrganizationId(user.getId());
		String note="";
		PersonState personState = new PersonState(null, id, user.getId(), user.getName(), DateUtils.getSystemTime(),
				emergency.getName(), emergency.getIdentify(), "", note, emergency.getCreaterDepartment(), "临时救助");

		emergency.setAudit(audit);
		emergency.setPeople(people);
		emergency.setMonth(month);
		emergency.setStandard(standard);
		emergency.setMoney(money);
		Emergency.editToAudit(emergency);
		if ("21".equals(userOragantion.getOrganizationId())) {// 如果乡镇街申报状态为待付款、如民政局申报状态为待支付
			emergency.setState("未支付");
			if (Emergency.editToState(emergency) == 1) {

				personState.setState(emergency.getState());
				PersonState.add(personState);
				return new JspView("/app/sldb/approval/pending/show.jsp");
			}
		} else {
			if ("待付款".equals(Emergency.get(id).getState())) {
				return new JspView("/app/sldb/account/error.jsp");
			} else {
				emergency.setState("待付款");
				if (Emergency.editToState(emergency) == 1) {

					personState.setState(emergency.getState());
					PersonState.add(personState);
					return new JspView("/app/sldb/account/country.jsp");
				}
			}
		}
		return new JspErrorView("信息更新失败");
	}

	@Override
	public String requestMapping() {
		// TODO Auto-generated method stub
		return "/app/sldb/emergency/approval/saveAndApplyEmergency.action";
	}
}
